package com.shigen.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shigen.demo.entity.UpdateLog;
import com.shigen.demo.entity.req.UpdateLogReq.QueryItem;
import com.shigen.demo.mapper.UpdateLogMapper;
import com.shigen.demo.service.IUpdateLogService;
import org.springframework.stereotype.Service;

@Service
public class UpdateLogServiceImpl extends ServiceImpl<UpdateLogMapper, UpdateLog> implements
    IUpdateLogService {

  /**
   * 根据作者或者更新内容进行模糊分页查询
   *
   * @param queryItem
   * @return
   */
  @Override
  public IPage<UpdateLog> getUpdateLogs(QueryItem queryItem) {
    Page<UpdateLog> page = new Page<>(queryItem.getPagenum(), queryItem.getPagesize());
    LambdaQueryChainWrapper<UpdateLog> lambdaQuery = lambdaQuery();
    String param = queryItem.getParam();

    if (StrUtil.isNotBlank(param)) {
      lambdaQuery.likeRight(UpdateLog::getAuthor, param).or().likeRight(UpdateLog::getContent, param);
    }
    return lambdaQuery.orderByDesc(UpdateLog::getTimestamp).page(page);
  }
}
